﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["act"] != null)
                {
                    string strAct = Request.QueryString["act"].ToString();
                    if (strAct.Equals("loginout"))
                    {
                        Session.RemoveAll();
                    }
                }
            }
        }

        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            lbErr.Text = "";
            Object isVal = new Object();
            string url = "~/Index.aspx";

            List<LabMS.DAL.People> peoList = new List<LabMS.DAL.People>();            
            
            if (DDLType.SelectedValue == "0")
            {
                peoList.Add(new DAL.UserTable());
                Session["UserType"] = "mana";
            }
            else if (DDLType.SelectedValue == "1")//老师
            {
                peoList.Add(new DAL.Teacher());
                Session["UserType"] = "teacher";
                url = "~/Teacher.aspx";
            }
            else if (DDLType.SelectedValue == "2")//学生
            {
                peoList.Add(new DAL.Student());
                Session["UserType"] = "student";
                url = "~/Student.aspx";
            }
            if(peoList.Count ==1)
            {
                try
                {
                    isVal = peoList[0].ValidatePeople(txtUserName.Value.Trim(), txtUserPass.Value.Trim());
                }
                catch(Exception ex)
                {
                    lbErr.Visible = true;
                    lbErr.Text = ex.Message;
                    return;
                }                   
            }
            else
            {
                lbErr.Visible = true;
                lbErr.Text = "程序错误!";
                return;
            }
            if (isVal!=null)
            {
                Session["User"] = isVal;
                Response.Redirect(url);
            }
            else
            {
                lbErr.Visible = true;
                lbErr.Text = "用户名\\密码错误!";
                return;
            }
        }
    }
}
